-- thrown knife

if SERVER then
   AddCSLuaFile("shared.lua")
else
   ENT.PrintName = "rpgdm_proj"
end



ENT.Type = "anim"
ENT.Model = Model("models/props_junk/Rock001a.mdl")
ENT.NewModel = nil
ENT.Damage = 0
ENT.ProjSpeed = 300
ENT.Owner = nil
ENT.Target = nil
ENT.TravelEffect = ""
ENT.TravelSound = ""
ENT.Travelling = false
ENT.HitEffect = ""
ENT.HitSound = ""
ENT.LifeTime = nil
ENT.ProjSound = nil

if SERVER then

function ENT:Initialize()
	if(self.NewModel != nil)then
		self.Entity:SetModel(self.NewModel)
		self.Entity:SetNoDraw(false)
	else
		self.Entity:SetModel(self.Model)
		self.Entity:SetNoDraw(true)
	end
	self.Entity:SetSolid( SOLID_NONE )
	self.Entity:SetMoveType(MOVETYPE_NOCLIP)
	self.ProjSound = CreateSound( self.Entity, self.TravelSound )
		self.ProjSound:Play()
		self.ProjSound:ChangeVolume(1, 0)
	self.Travelling = true
	
	ParticleEffectAttach( self.TravelEffect, PATTACH_ABSORIGIN_FOLLOW, self.Entity, self.Entity:LookupAttachment("oh"))

	if SERVER then
		self:SetGravity(1)
		self.StartPos = self:GetPos()
		self.Entity:NextThink(CurTime()+0.001)
	end
	
	if(self.LifeTime != nil)then
		timer.Simple(self.LifeTime, function() 
			if(self:IsValid())then
				self:Remove() 
				self.ProjSound:Stop()
			end
		end)
	end
end

function ENT:HitTarget(ent)
	if(self.Damage > 0)then
		ParticleEffect( self.HitEffect, self.Target:GetPos() + Vector(0, 0, 46), Angle(0, 0, 0), self.Target )
		self.Entity:EmitSound(self.HitSound, 100, 100+math.random(-10,10))
		self.ProjSound:Stop()
		local dmginfo_proj = DamageInfo()
			dmginfo_proj:SetDamage( self.Damage ) --50 damage
			dmginfo_proj:SetDamageType( DMG_SLASH ) --Bullet damage
			dmginfo_proj:SetAttacker( self.Owner ) --First player found gets credit
			dmginfo_proj:SetDamageForce( Vector( 0, 0, 1000 ) ) --Launch upwards
		self.Target:TakeDamageInfo( dmginfo_proj )
	end
	self.ProjSound:Stop()
	self:Remove()
end
function ENT:Think()
	if self.ChangeDir then
		self.ChangeDir = false
		self.Entity:SetMoveType(MOVETYPE_FLY)
		timer.Simple(0.5, function()
			self.Entity:SetMoveType(MOVETYPE_WALK)
		end)
	end
	if(self.Travelling)then
		if IsValid(self.Target) and !self.IsGrapple and !self.IsSword then
			self.Entity:SetAngles((self.Target:GetPos()+Vector(0, 0, 48) - self:GetPos()):Angle())
			self:SetVelocity(self:GetAngles():Forward()*self.ProjSpeed)
		elseif IsValid(self.Target) and self.IsGrapple and !self.IsSword then
			self.Entity:SetAngles((self.Target:GetPos()+Vector(0, 0, 48) - self:GetPos()):Angle() + Angle(0, -90, 90))
			self:SetVelocity(self:GetAngles():Forward()*-self.ProjSpeed)
		elseif IsValid(self.Target) and !self.IsGrapple and self.IsSword then
			self.Entity:SetAngles((self.Target:GetPos()+Vector(0, 0, 48) - self:GetPos()):Angle() + Angle(180, 0, 0))
			self:SetVelocity(self:GetAngles():Forward()*-self.ProjSpeed)
		else
			if( self.ProjSound )then
				self.ProjSound:Stop()
			end
			self:Remove()
		end
	else
		if((self.IsGrapple or self.IsSword))then
			self.Entity:SetMoveType(MOVETYPE_NONE)
			self.Entity:SetPos(self:GetParent():GetPos() + Vector(0, 0, 48) - (self.Entity:GetAngles():Forward()*-30))
		end
	end
	self.Entity:NextThink(CurTime()+0.000001)
end

function ENT:StartTouch(ent)
	print("Projectile hit something")
	
	if ent == self.Target then
		self:HitTarget(ent)
	end
	
	if ent:IsWorld() then
		self.ProjSound:Stop()
		ParticleEffect( self.HitEffect, self:GetPos() + Vector(0, 0, 46), Angle(0, 0, 0), self )
		self.Entity:EmitSound(self.HitSound, 100, 100+math.random(-10,10))
		self:Remove()
	end
	
	return true
end

end


